﻿using System;
using System.Collections.Generic;
using System.Text;

namespace ITAP.ShprossesBuilder.Modelling.Components.Constraints
{
    [Serializable]
    public class ComponentCreateSpacerConnectorOnFreeEndConstraint : IConstraint
    {
        private bool enabled = true;
        #region IConstraint Members

        public bool Check(IComponent component)
        {
            // Пока временно сделал так, чтобы сэкономить процессорное время (ест слишком много ресурсов)
            return false;

            if (!(component is IEndedComponent)) return true;

            IEndedComponent endedComponent = component as IEndedComponent;


            if (!this.IsCorrect(endedComponent, endedComponent.StartConnectedComponent))
                return false;

            if (!this.IsCorrect(endedComponent, endedComponent.EndConnectedComponent))
                return false;

            return true;
        }

        private bool IsCorrect(IEndedComponent component, IComponent componentOnEnd)
        {
            if (componentOnEnd == null)
                return false;

            return true;
            //if (component.ConnectPriorityForEnds.IndexOf(typeof(SpacerConnector)) > component.ConnectPriorityForEnds.IndexOf(componentOnEnd.GetType()))
            //if(componentOnEnd.GetType() != typeof(SpacerConnector))
            //    return false;
            //else
            //    return true;
        }

        private void RemoveEndComponent(IComponent component)
        {
            if (component == null)
                return;

            component.Parent.RemoveInnerComponent(component);
            component.DisconnectFromAll();
            component = null;
        }

        public void Fix(IComponent component)
        {
            if (!enabled) return;
            if (!(component is IEndedComponent)) return;

            //IEndedComponent endedComponent = component as IEndedComponent;
            Shpross endedComponent = component as Shpross;
            if (!this.IsCorrect(endedComponent, endedComponent.StartConnectedComponent))
            {
                endedComponent.FitStartSpacerConnector();
                //this.RemoveEndComponent(endedComponent.StartConnectedComponent);
                //SpacerConnector startSpacerConnector = (SpacerConnector)endedComponent.StartSpacerConnector.Clone(); // ComponentFactory.CreateDefaultSpacerConnector(endedComponent.StartPoint, endedComponent.Primitives[0].GetTangent(0), "StartSpacerConnector");
                //endedComponent.AddInnerComponent(startSpacerConnector);
                //ConnectionProvider.Connect(endedComponent, startSpacerConnector, Shprosses.Modelling.Primitives.EndType.Start, Shprosses.Modelling.Primitives.EndType.End);
            }

            if (!this.IsCorrect(endedComponent, endedComponent.EndConnectedComponent))
            {
                endedComponent.FitEndSpacerConnector();
                //this.RemoveEndComponent(endedComponent.EndConnectedComponent);

                //SpacerConnector endSpacerConnector = (SpacerConnector)endedComponent.EndSpacerConnector.Clone(); //ComponentFactory.CreateDefaultSpacerConnector(endedComponent.EndPoint, endedComponent.Primitives[0].GetTangent(1).Rotate(new Angle(Math.PI)), "EndSpacerConnector");
                //endedComponent.AddInnerComponent(endSpacerConnector);
                //ConnectionProvider.Connect(endedComponent, endSpacerConnector, Shprosses.Modelling.Primitives.EndType.End, Shprosses.Modelling.Primitives.EndType.End);
            }
        }

        public bool Enabled
        {
            get
            {
                return enabled;
            }
            set
            {
                enabled = value;
            }
        }

        public ConstraintAction Action
        {
            get { return ConstraintAction.Fix; }
        }

        #endregion

        #region ICloneable Members

        public object Clone()
        {
            ComponentCreateSpacerConnectorOnFreeEndConstraint clone = new ComponentCreateSpacerConnectorOnFreeEndConstraint();
            return clone;
        }

        #endregion
    }
}
